package test;

import java.security.interfaces.RSAPublicKey;

import com.demo.support.util.CertificateUtil;
import com.demo.support.util.RSAUtil;
import com.jweb.annotaion.JController;
import com.jweb.core.handle.Controller;
import com.jweb.util.SecurityUtil;
import com.jweb.util.rsa.RsaCodingUtil;

@JController("/test/rsa")
public class RSAController extends Controller {

	/**
	 * RSA 加解密测试
	 */
	public void page() {
		RSAPublicKey publicKey = (RSAPublicKey) RSAUtil.getPublicKey();

		setAttr("encryptionExponent", CertificateUtil.bytesToHexStr(publicKey.getPublicExponent().toByteArray()));
		setAttr("modulus", CertificateUtil.bytesToHexStr(publicKey.getModulus().toByteArray()));
	}

	/**
	 * 后端进行解密 
	 */
	public void rsaDecry() {
		try {
			String base64 = RsaCodingUtil.decryptByPrivateKey(getParameter("value"), RSAUtil.getPrivateKey());
			toWriterText(SecurityUtil.Base64Decode(base64));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
